<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\common;

use app\api\model\dao\Picture;
use app\library\aliyun\oss\OssWorker;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Log;

class UploadImage extends PageBase
{

    private $objOss;
    private $objPicture;

    public function __construct()
    {
        parent::__construct();
        $this->objOss = new OssWorker();
        $this->objPicture = new Picture();
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            //return $this->getRes(Error::ERR_NO_AUTH);
        }
        if (empty($param['type'])) {
            $param['type'] = Flag::CONST_PICTURE_NOTE;
        }

        if (!isset($_FILES["file"]) || empty($_FILES["file"])) {
            return $this->getRes(Error::ERR_SYS, 'file is empty...');
        }

        $file = $_FILES["file"];

        // 错误判断
        if ($file['error'] > 0) {
            switch ($file['error']) {
                case 1:
                    $this->errno = Error::ERR_FILE_LARGER_THAN_SERVER;
                    break;
                case 2:
                    $this->errno = Error::ERR_FILE_LARGER;
                    break;
                case 3:
                    $this->errno = Error::ERR_FILE_UNCOMPLETE;
                    break;
                case 4:
                    $this->errno = Error::ERR_FILE_LOAD;
                    break;
                default:
                    $this->errno = Error::ERR_PARAM;
                    break;
            }
            return $this->getRes(Error::ERR_SYS, 'check file fail...');
        }

        // 大小判断
        if ($file['size'] > (Flag::IMGMAXWIDTH * Flag::IMGMAXHIGHT)) {
            Log::error(__METHOD__ . " uploadimage is too large.filetsize:{$file['size']}.arrInput:" . json_encode($param));
            return $this->getRes(Error::ERR_FILE_TOO_LARGE);
        }

        // 类型判断
        $tp = array("image/jpg", "image/gif", "image/jpeg", "image/png", "image/pjpeg");
        if (!in_array($file["type"], $tp)) {
            Log::error(__METHOD__ . " uploadimage filetype failed.filetype:{$file['type']}.arrInput:" . json_encode($param));
            return $this->getRes(Error::ERR_FILE_TYPE_ERROR);
        }

        // 文件md5是否正确，MD5校验先关了
        $md5file = md5_file($file['tmp_name']);
//        if($md5file != $param['md5']) {
//            return $this->getRes('check file MD5 fail...');
//        }

        $ret = explode('.', $file['name']);
        $suffix = end($ret);
        $object_name = "$md5file.$suffix";//文件名
        $file_name = $file['tmp_name'];

        $date = date('Ymd');
        $username = Flag::$map_pic[$param['type']] . '/' . $date;

        if ($param['type'] == Flag::CONST_PICTURE_GOODS) {
            $image = \think\Image::open($file_name);
            $scale = $image->width() / 750;
            $image->thumb(750, $image->height() * $scale)->save($file_name);
        }


        // 上传文件
        $ret = $this->objOss->uploadFile($username, $object_name, $file_name);
        if (false === $ret) {
            $ret = $this->objOss->uploadFile($username, $object_name, $file_name);
            if (false === $ret) {
                return $this->getRes(Error::ERR_FILE_UPLOAD_FAIL);
            }
        }

        // 添加到DB中记录上传图片
        $input = [
            //'userid' => $this->user_id,
            'type' => $param['type'],
            'obj_id' => 0,
            'url' => $ret,
            'create_time' => time(),
        ];
        if (isset($this->user_id)) {
            $input['userid'] = $this->user_id;
        }
        $this->objPicture->addPicture($input);
        $this->data['url'] = $ret;
        return $this->getRes();
    }
}
